<?php

class Novel_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    /**
     * Model to get all manga available
     * @return type 
     */
    function getNovel_all() {
        $query = $this->db->order_by("name", "asc")->get('novel');
        $arr = array();
        foreach ($query->result() as $row) {
            $row->cats = $this->getCategory_of_novel($row->id);
            $arr[] = $row;
        }
        return $arr;
    }
    
    function getNovel_newest($start = 0, $count = 12) {
        $query = $this->db->order_by("created", "desc")->limit($count, $start)->get('novel');

        $arr = array();
        foreach ($query->result() as $row) {
            $row->cats = $this->getCategory_of_novel($row->id);
            $arr[] = $row;
        }
        return $arr;
    }

    function getNovelInfo_by_id($id) {
        $novel = $this->db->get_where('novel', array('id' => $id))->row();
        $novel->cats = $this->getCategory_of_novel($id);
        return $novel;
    }
    
    function getNovel_by_slug($slug) {
        $novel = $this->db->get_where('novel', array('slug' => $slug))->row();
        $novel->cats = $this->getCategory_of_novel($novel->id);
        return $novel;
    }
    
    function getNovelChapter_by_id($id) {
        $chapter = $this->mongo_db->where('novelId', $id)->order_by(array("chapter" => "asc"))->get('novel');
        usort($chapter, 'usort_chapter');
        return $chapter;
    }
    
    function getCategory_of_novel($novelId) {
        $sql = 'SELECT c.* FROM category c, relationship r WHERE c.id = r.cid AND r.oid=' . $novelId;
        $query = $this->db->query($sql);
        $arr = array();
        foreach ($query->result() as $row) {
            $arr[] = $row;
        }
        return $arr;    
    }
    
    function getNovel_by_category($cateId, $start = 0, $limit = 6) {
        $sql = 'SELECT c.id as cat_id, c.name as cat_name, c.slug as cat_slug, n.* FROM novel n, category c, relationship r WHERE n.id = r.oid AND c.id = r.cid AND c.id=' . $cateId . ' LIMIT ' . $start . ', ' . $limit;
        $query = $this->db->query($sql);
        $arr = array();
        foreach ($query->result() as $row) {
            $row->cats = $this->getCategory_of_novel($row->id);
            $arr[] = $row;
        }
        return $arr; 
    }

    function getNovel_slug_by_id($novelId) {
        return $this->db->select('slug')->get_where('novel', array('id' => $novelId))->row()->slug;
    }
    
    function getNovel_id_by_slug($slug) {
        return $this->db->select('id')->get_where('novel', array('slug' => $slug))->row()->id;
    }
    
    /**
     * Check a manga is exist or not
     * 
     * @param type $id
     * @return type 
     */
    private function checkNovelIdExist($id = 0) {
        $query_u = $this->db->get_where('novel', array('id' => $id));
        return $query_u->num_rows > 0;
    }
    
    /**
     * Get chapter detail of a manga
     * 
     * @param type $mangaId: manga Id
     * @param type $chapterId: chapter Id
     * @return type 
     */
    function getChapter_by_chapterId($novelId, $chapterId) {
        $data = $this->mongo_db->where(array('novelId' => $novelId, 'chapter' => $chapterId))->get('novel');
        return $data;
    }
    
    function getChapter_lastest($start = 0, $count = 12) {
        $end = strtotime('-3 day', strtotime('00:00:00'));
        $datas = $this->mongo_db->select(array('novelId', 'chapter', 'created'))
                        ->order_by(array("created" => "desc"))
                        ->limit($count, $start)
                        ->get('novel');
        return $datas;
    }
}

?>
